package com.itheima.util;

import java.sql.*;
import java.util.Scanner;

public class JdbcDemo03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        boolean flag = login(username, password);
        if (flag) {
            System.out.println("登陆成功!");
        } else {
            System.out.println("用户名或密码错误!");
        }
    }

    public static boolean login(String username, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        if (username == null && password == null) {
            return false;
        }
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select * from user where username=?and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            /*if (rs.next()) {
                return true;
            } else {
                return false;
            }*/
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils.close(conn, pstmt, rs);
        }
        return false;
    }
}
